using KinonekoSoftware.Studio.Models.Theories;
using KinonekoSoftware.Studio.Pages.Theories.Rarities;

namespace KinonekoSoftware.Studio.Pages.Theories
{
    partial class WorldViewConfigTheoryViewModel
    {
        

        //-------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------

        private async void DoNewRarityCommand()
        {
            var r = await NewRarityViewModel.Create();

            if (!r.IsFinished)
            {
                return;
            }

            var val = r.Value;

            //
            //
            _theory_rarity.Add(val);

            //
            //
            Rarities.Add(TheoryOfRarityPresenter.Select(val));

            //
            //
            Save();
        }

        private async void DoEditRarityCommand(TheoryOfRarityPresenter element)
        {
            if (element is null)
            {
                return;
            }

            var r = await NewRarityViewModel.Edit(element.TargetObject);

            if (!r.IsFinished)
            {
                return;
            }

            //
            //
            element.RaiseUpdates();
            _theory_rarity.Update(element.TargetObject);

            //
            //
            Save();
        }

        private async void DoResetRarityCommand()
        {
            if (!await DangerOperation(TitleSR.DangerOperation, TextSR.Remove_Item_All))
            {
                return;
            }
            
            
            
            Rarities.Clear();
            _theory_rarity.Clear();
            
            //
            Save();
        }

        private async void DoRemoveRarityCommand(TheoryOfRarityPresenter element)
        {
            if (element is null)
            {
                return;
            }

            if (!await RemoveItem())
            {
                return;
            }

            Rarities.Remove(element);
            _theory_rarity.Remove(element.TargetObject);

            Save();
        }



        public ICommandEX NewRarityCommand    { get; }
        public ICommandEX EditRarityCommand   { get; }
        public ICommandEX ResetRarityCommand  { get; }
        public ICommandEX RemoveRarityCommand { get; }

        public ObservableCollection<TheoryOfRarityPresenter> Rarities       { get; }
    }
}